home *** CD-ROM | disk | FTP | other *** search
/ 10,000 Great Games / 10,000 Great Games.iso / Product / 66 / data1.cab / Source_Files / Src / Dsound.cpp < prev    next >
C/C++ Source or Header  |  2000-01-16  |  1KB  |  80 lines

  1. #include "stdafx.h"
  2.  
  3. LPDIRECTSOUND DS = 0;
  4. LPDIRECTSOUNDBUFFER soundcard = 0;
  5. int no_sound = FALSE;
  6.  
  7. void init_directsound()
  8. {
  9.     // Get DirectSound object
  10.     
  11.     if (no_sound || FAILED(DirectSoundCreate(0, &DS, 0)))
  12.     {
  13.         no_sound = TRUE;
  14.         return;
  15.     }
  16. }
  17.  
  18. void init_directsound_primarybuffer()
  19. {    
  20.     if (no_sound)
  21.         return;
  22.  
  23.     // Set cooperative level
  24.     
  25.     if(FAILED(DS->SetCooperativeLevel(mainwindowhandle, DSSCL_NORMAL)))
  26.         error("Unable to set cooperative level for sound card");
  27.     
  28.     // Create primary buffer
  29.     
  30.     DSBUFFERDESC dsbd;
  31.     
  32.     ZeroMemory(&dsbd, sizeof(dsbd));
  33.     dsbd.dwSize = sizeof(dsbd);
  34.     dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
  35.     
  36.     if (FAILED(DS->CreateSoundBuffer(&dsbd, &soundcard, 0)))
  37.         error("Unable to create primary sound buffer");
  38.     
  39.     // Start playing silence
  40.     
  41.     soundcard->Play(0, 0, DSBPLAY_LOOPING);
  42. }
  43.  
  44. void deinit_directsound()
  45. {
  46.     safe_release(&soundcard);
  47.     safe_release(&DS);
  48. }
  49.  
  50. void play_sound(cWAV *snd, int volume)
  51. {
  52.     if (no_sound)
  53.         return;
  54.     
  55.     snd->dsb->SetCurrentPosition(0);    
  56.     snd->dsb->SetVolume(volume);
  57.     snd->dsb->Play(0,0,0);        
  58. }
  59.  
  60. void ambient_sounds()
  61. {
  62.     static cTimer wait;
  63.     
  64.     if (!wait && !no_sound)
  65.     {
  66.         switch(rnd(2))
  67.         {
  68.         case 0:
  69.             play_sound(cWAV::get("ALARM"));
  70.             break;
  71.             
  72.         case 1:
  73.             play_sound(cWAV::get("CREAK"));
  74.             break;
  75.         }
  76.         
  77.         wait = rnd(40 * sec);
  78.     }
  79. }
  80.